﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMSMVC4.Areas.Admin.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Admin.Controllers
{
    [Authorize(Roles = "Admin,Service")]
    public class ServicesController : Controller
    {
        //
        // GET: /Admin/Service/List
        CMSMVC4Entities db = new CMSMVC4Entities();
        CommandSql cm = new CommandSql();

        public ActionResult Sort(int id)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/DanhMuc").Rows[0]["Parrent"];
            string curte = cm.GetLanguage();
            var data = db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(x => x.Category.Type == MenuType.DichVu && x.LanguageCode.Equals(curte) && x.Category.Status == CategoriStatus.Thuong && x.Category.paren == id).ToList();
            ViewBag.id = id;
            return View(data);
        }

        [HttpGet]
        public ActionResult List()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/List").Rows[0]["Parrent"];
            var data = db.Categories.Where(x => x.Type == MenuType.DichVu && x.Status == CategoriStatus.Thuong).ToList();
            return View(data);
        }

        #region
        [HttpGet]
        public ActionResult Add(string id)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/Add").Rows[0]["Parrent"];
            int idcate;
            Category ctf;
            if (int.TryParse(id,out idcate))
            {
                ctf = db.Categories.FirstOrDefault(x => x.Id == idcate);
                if (ctf == null)
                {
                    return View("Error");
                }
            }
            else
            {
                ctf = db.Categories.FirstOrDefault(x => x.Type == MenuType.DichVu && x.Status == CategoriStatus.Nhap);
                if (ctf == null)
                {
                    int sort = 0;
                    var data = db.Categories.OrderByDescending(x=>x.Sort).FirstOrDefault(x => x.Type == MenuType.DichVu && x.Status == CategoriStatus.Thuong);
                    if (data != null)
                    {
                        if (data.Sort != null) sort = (int)data.Sort + 1;
                    }
                    var ct = new Category {Type = MenuType.DichVu, Status = CategoriStatus.Nhap, Sort = sort};
                    db.Categories.AddObject(ct);
                    db.SaveChanges();
                    ctf = db.Categories.OrderByDescending(x => x.Id).FirstOrDefault(x => x.Type == MenuType.DichVu && x.Status == CategoriStatus.Nhap);
                }
            }
            var catelagv = db.CateLangs.FirstOrDefault(x => x.CategoriesId == ctf.Id && x.LanguageCode.Equals("vi-vn"));
            var catelage = db.CateLangs.FirstOrDefault(x => x.CategoriesId == ctf.Id && x.LanguageCode.Equals("en-us"));
            if (catelagv == null)
            {
                if (ctf != null)
                {
                    var ctl = new CateLang {CategoriesId = ctf.Id, Name = "Tên Dịch Vụ", LanguageCode = "vi-vn"};
                    db.CateLangs.AddObject(ctl);
                }
                db.SaveChanges();
                catelagv = db.CateLangs.FirstOrDefault(x => x.CategoriesId == ctf.Id && x.LanguageCode.Equals("vi-vn"));
            }
            if (catelage == null)
            {
                if (ctf != null)
                {
                    var ctl = new CateLang {CategoriesId = ctf.Id, Name = "Name Service", LanguageCode = "en-us"};
                    db.CateLangs.AddObject(ctl);
                }
                db.SaveChanges();
                catelage = db.CateLangs.FirstOrDefault(x => x.CategoriesId == ctf.Id && x.LanguageCode.Equals("en-us"));
            }
            var page = db.Pages.FirstOrDefault(x => x.CategoriesID == ctf.Id);
            if (page == null)
            {
                if (ctf != null)
                {
                    var pn = new Page {CategoriesID = ctf.Id, Status = PageStatus.Thuong, Tab = 1, View = 0};
                    db.Pages.AddObject(pn);
                }
                db.SaveChanges();
                page = db.Pages.FirstOrDefault(x => x.CategoriesID == ctf.Id);
            }
            var pagelangv = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("vi-vn"));
            var pagelange = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("en-us"));
            if (pagelangv == null)
            {
                if (page != null)
                {
                    var pgn = new PageLang {Name = "Tên Bài Viết", LanguageCode = "vi-vn", PageId = page.Id};
                    db.PageLangs.AddObject(pgn);
                }
                db.SaveChanges();
                pagelangv = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("vi-vn"));
            }
            if (pagelange == null)
            {
                if (page != null)
                {
                    var pgn = new PageLang {Name = "Name", LanguageCode = "en-us", PageId = page.Id};
                    db.PageLangs.AddObject(pgn);
                }
                db.SaveChanges();
                pagelange = db.PageLangs.FirstOrDefault(x => x.PageId == page.Id && x.LanguageCode.Equals("en-us"));
            }
            var model = new AddServicesModel
                            {
                                idcate = ctf.Id,
                                fulle = pagelange.FullContent,
                                fullv = pagelangv.FullContent,
                                idpqge = page.Id,
                                namecatee = catelage.Name,
                                namecatev = catelagv.Name,
                                namepagee = pagelange.Name,
                                namepagev = pagelangv.Name,
                                sume = pagelange.Summary,
                                sumv = pagelangv.Summary,
                                idcatelage = catelage.Id,
                                idcatelagv = catelagv.Id,
                                idpagelage = pagelange.Id,
                                idpagelagv = pagelangv.Id
                            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Add(AddServicesModel model)
        {
            if (ModelState.IsValid)
            {
                ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/Add").Rows[0]["Parrent"];
                var cate = db.Categories.FirstOrDefault(x => x.Id == model.idcate);
                if (cate != null)
                {
                    cate.Status = CategoriStatus.Thuong;
                    cate.paren = model.paren;
                    int sort = 0;
                    var data = db.Categories.OrderByDescending(x => x.Sort).FirstOrDefault(x => x.Type == MenuType.DichVu && x.Status == CategoriStatus.Thuong && x.paren == model.paren);
                    if (data != null)
                    {
                        sort = (int)data.Sort + 1;
                    }
                    cate.Sort = sort;
                }
                var catelagv = db.CateLangs.FirstOrDefault(x => x.Id == model.idcatelagv);
                var catelage = db.CateLangs.FirstOrDefault(x => x.Id == model.idcatelage);
                if (catelagv != null)
                {
                    catelagv.Name = model.namecatev;
                }
                if (catelage != null)
                {
                    catelage.Name = model.namecatee;
                }
                var pagelagv = db.PageLangs.FirstOrDefault(x => x.Id == model.idpagelagv);
                var pagelage = db.PageLangs.FirstOrDefault(x => x.Id == model.idpagelage);
                if (pagelagv != null)
                {
                    pagelagv.Name = model.namepagev;
                    pagelagv.FullContent = model.fullv;
                    pagelagv.Summary = model.sumv;
                }
                if (pagelage != null)
                {
                    pagelage.Name = model.namepagee;
                    pagelage.FullContent = model.fulle;
                    pagelage.Summary = model.sume;
                }
                db.SaveChanges();
// ReSharper disable LocalizableElement
                ModelState.AddModelError("","succsessfull");
// ReSharper restore LocalizableElement
            }
            return View(model);
        }
        #endregion

        [ChildActionOnly]
        public ActionResult GetNameCate(int id)
        {
            Page page = db.Pages.FirstOrDefault(x => x.CategoriesID == id && x.Status == PageStatus.GioiThieu);
            string curte = cm.GetLanguage();
            CateLang data = db.CateLangs.FirstOrDefault(x => x.CategoriesId == id && x.LanguageCode.Equals(curte));
            if (data != null)
            {
                ViewBag.data = data.Name;
            }
            if (page == null)
            {
                page = db.Pages.FirstOrDefault(x => x.CategoriesID == id);
                if (page != null)
                {
                    ViewBag.pageid = page.Id;
                }
            }
            return PartialView();
        }

        public ActionResult Delete(string[] id)
        {
            if (id != null)
            {
                foreach (var i in id)
                {
                    int idcomment;
                    int.TryParse(i, out idcomment);
                    var row = db.Categories.FirstOrDefault(x => x.Id == idcomment);
                    db.Categories.DeleteObject(row);
                    db.SaveChanges();
                }
            }
            return Json(string.Empty, JsonRequestBehavior.AllowGet);
        }

        public ActionResult DanhMuc()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/DanhMuc").Rows[0]["Parrent"];
            string curte = cm.GetLanguage();
            var data = db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(x => x.Category.Type == MenuType.DichVu && x.LanguageCode.Equals(curte) && x.Category.Status == CategoriStatus.Foder).ToList();
            return View(data);
        }

        [HttpPost]
        public ActionResult XoaDanhMuc(string[] id)
        {
            if (id != null)
            {
                foreach (var i in id)
                {
                    int idcomment;
                    int.TryParse(i, out idcomment);
                    var row = db.Categories.FirstOrDefault(x => x.Id == idcomment);
                    if (row != null)
                    {
                        var cate = db.Categories.Where(x => x.paren == row.Id);
                        foreach (var item in cate)
                        {
                            var chiren = db.Categories.FirstOrDefault(x => x.Id == item.Id);
                            if (chiren != null)
                            {
                                db.Categories.DeleteObject(chiren);
                            }
                        }
                        db.Categories.DeleteObject(row);
                        db.SaveChanges();
                    }
                }
            }
            return Json(string.Empty, JsonRequestBehavior.AllowGet);
        }

        public ActionResult SortFolder()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/DanhMuc").Rows[0]["Parrent"];
            string curte = cm.GetLanguage();
            var data = db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(x => x.Category.Type == MenuType.DichVu && x.LanguageCode.Equals(curte) && x.Category.Status == CategoriStatus.Foder).ToList();
            return View(data);
        }

        public ActionResult ThemDanhMuc()
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/DanhMuc").Rows[0]["Parrent"];
            return View();
        }

        [HttpPost]
        public ActionResult ThemDanhMuc(ThemDanhMucModel model)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/DanhMuc").Rows[0]["Parrent"];
            if (ModelState.IsValid)
            {
                int sort = 0;
                var cate =
                    db.Categories.OrderByDescending(x => x.Sort).FirstOrDefault(x => x.Status == CategoriStatus.Foder && x.Type == MenuType.DichVu);
                if (cate != null)
                {
                    var i = cate.Sort + 1;
                    if (i != null) sort = (int)i;
                }
                Category row = new Category();
                row.Sort = sort;
                row.Status = CategoriStatus.Foder;
                row.Type = MenuType.DichVu;
                row.paren = 0;
                db.Categories.AddObject(row);
                db.SaveChanges();
                cate =
                    db.Categories.OrderByDescending(x => x.Sort).FirstOrDefault(x => x.Status == CategoriStatus.Foder && x.Type == MenuType.DichVu);
                if (cate != null)
                {
                    CateLang clv = new CateLang();
                    clv.CategoriesId = cate.Id;
                    clv.LanguageCode = "vi-vn";
                    clv.Name = model.namecatev;
                    db.CateLangs.AddObject(clv);
                    CateLang cle = new CateLang();
                    cle.CategoriesId = cate.Id;
                    cle.LanguageCode = "en-us";
                    cle.Name = model.namecatev;
                    db.CateLangs.AddObject(cle);
                    db.SaveChanges();
                    return RedirectToAction("DanhMuc");
                }
            }
            return View();
        }

        public ActionResult SuaDanhMuc(int id)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/DanhMuc").Rows[0]["Parrent"];
            var clv = db.CateLangs.FirstOrDefault(x => x.CategoriesId == id && x.LanguageCode.Equals("vi-vn"));
            var cle = db.CateLangs.FirstOrDefault(x => x.CategoriesId == id && x.LanguageCode.Equals("en-us"));
            SuaDanhMucModel model = new SuaDanhMucModel();
            if (clv != null)
            {
                model.idv = clv.Id;
                model.namecatev = clv.Name;
            }
            if (cle != null)
            {
                model.ide = cle.Id;
                model.namecatee = cle.Name;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult SuaDanhMuc(SuaDanhMucModel model)
        {
            ViewBag.M = new CommandSql().GetParrentByMenu("/Admin/Services/DanhMuc").Rows[0]["Parrent"];
            if (ModelState.IsValid)
            {
                var clv = db.CateLangs.FirstOrDefault(x => x.Id == model.idv);
                if (clv != null)
                {
                    clv.Name = model.namecatev;
                    db.SaveChanges();
                }
                var cle = db.CateLangs.FirstOrDefault(x => x.Id == model.ide);
                if (cle != null)
                {
                    cle.Name = model.namecatee;
                    db.SaveChanges();
                }
                ModelState.AddModelError("", "Thay Đổi Dữ Liệu Thành Công");
            }
            return View(model);
        }

        [ChildActionOnly]
        public ActionResult DanhSachDanhMuc(int id)
        {
            string lag = cm.GetLanguage();
            var data = db.CateLangs.OrderByDescending(x => x.Category.Sort).Where(x => x.LanguageCode.Equals(lag) && x.Category.Status == CategoriStatus.Foder && x.Category.Type == MenuType.DichVu).ToList();
            List<ItemData> lst = new List<ItemData>();
            foreach (var item in data)
            {
                ItemData ida = new ItemData();
                ida.name = item.Name;
                ida.value = item.CategoriesId;
                lst.Add(ida);
            }
            ItemData itemnew = new ItemData();
            itemnew.name = "Không Dùng Danh Mục";
            itemnew.value = 0;
            lst.Insert(0, itemnew);
            SelectList list = new SelectList(lst, "value", "name");
            DanhSachDanhMucModel model = new DanhSachDanhMucModel();
            model.data = list;
            model.paren = id;
            return PartialView(model);
        }

    }
}
